//
//  HPSetUrlViewController.m
//  HPZZTong
//
//  Created by handpay on 14-12-5.
//  Copyright (c) 2014年 handpay. All rights reserved.
//

#import "HPSetUrlViewController.h"

@interface HPSetUrlViewController ()
@property (retain, nonatomic) IBOutlet UIButton *confirmButton;
@property (retain, nonatomic) IBOutlet UITableView *urlTableView;
@property(retain, nonatomic) NSArray* dataArray;
@property(retain, nonatomic) UITextField* urlTextField;
@property(nonatomic) NSUInteger selectIndex;
@end

@implementation HPSetUrlViewController

- (IBAction)buttonClick:(id)sender {
    NSString* urlStr = [[_dataArray objectAtIndex:_selectIndex] objectAtIndex:1];
    if (_selectIndex == 4) {
        urlStr = self.urlTextField.text;
    }
    [HPCLIENTENG SaveGlobalData:urlStr withKey:@"APPURL"];
    
    [ZZT_CLIENG.uiEngine removeSetUrlScreen];
    [HPUIENG showWelcomeScreen];
    
    if (HPCLIENTENG.hpAppDelegate) {
        [HPCLIENTENG.hpAppDelegate initApp];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = @[@[@"15环境", @"http://10.48.196.232:9580/hpaySft/"],
                       @[@"30环境", @"http://10.48.196.232:8380/hpaySft/"],
                       @[@"预发布环境",@"http://10.48.192.19:80/hpaySft/"],
                       @[@"生产环境",@"http://mps.handpay.cn/hpaySft/"],
                       @[@"其他",@"http:///hpaySft/"]];
    self.urlTableView.delegate = self;
    self.urlTableView.dataSource = self;
    
    [self.confirmButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.confirmButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    if (indexPath.row == 0) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    if (indexPath.row != (self.dataArray.count - 1)) {
        UILabel* desLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 130, 44)];
        desLabel.textColor = kNormalFontColor;
        desLabel.font = kNormalFont;
        desLabel.text = [[_dataArray objectAtIndex:indexPath.row] objectAtIndex:0];
        [cell addSubview:desLabel];
        [desLabel release];
    }else{
        UILabel* desLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 40, 44)];
        desLabel.textColor = kNormalFontColor;
        desLabel.font = kNormalFont;
        desLabel.text = [[_dataArray objectAtIndex:indexPath.row] objectAtIndex:0];
        [cell addSubview:desLabel];
        [desLabel release];
        
        UITextField* urlT = [[UITextField alloc]initWithFrame:CGRectMake(75, 0, 210, 44)];
        urlT.placeholder = @"请输入地址";
        urlT.font = kNormalFont;
        [cell addSubview:urlT];
        self.urlTextField = urlT;
        [urlT release];
    }
    
    return [cell autorelease];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if(indexPath.row == _selectIndex){
        return;
    }
    NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:_selectIndex
                                                   inSection:0];
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    if (newCell.accessoryType == UITableViewCellAccessoryNone) {
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        
    }
    _selectIndex = indexPath.row;
}
- (void)dealloc {
    self.urlTextField = nil;
    self.dataArray = nil;
    [_urlTableView release];
    [_confirmButton release];
    [super dealloc];
}
@end
